<?php
function bugreporter($errno, $errstr, $errfile = null, $errline = null,
        $errcontext = null) {
    if (strpos($errfile, '/smarty/') !== false) {
        return;
    }
    if ((strpos($errstr, 'fopen(') !== false) &&
            (strpos($errstr, 'http://') !== false)) {
        return;
    }
    if ((strpos($errstr, 'fopen(') !== false) &&
            (strpos($errstr, 'Success') || strpos($errstr, 'failed') ||
            strpos($errstr, 'Unable'))) {
        return;
    }
    $message = "Level: $errno\nDescription: $errstr\nFile: $errfile\nLine: " .
            "$errline\nRequest:\n" . print_r($_REQUEST, true) .
            "\nReferer:\n" . (isset($_SERVER['HTTP_REFERER']) ?
            $_SERVER['HTTP_REFERER'] : '') . "\nContext:\n" .
            print_r($errcontext, true) . "\n\n\n-------------------------\n" .
            "Backtrace:\n" . print_r(debug_backtrace(), true);
    if (class_exists('Modules')) {
        $dbapi = Modules::getEmergencyDBAPI();
        $dbdriver = $dbapi->getDriver();
        $dbdriver->delete('bugreport', '`reported` < ' .
                (time() - 86400));
        $bugRecord = array('table' => 'bugreport', 'properties' => array(
                'number' => 'iN', 'string' => "v'4096'N", 'file' => "v'4094'N",
                'line' => 'iN', 'reported' => 'iNI'), 'data' => array());
        $check = &$dbapi->load($bugRecord, "`number` = $errno " .
                "AND `string` = '" . $dbdriver->escapeString($errstr) . "' " .
                "AND `file` = '" . $dbdriver->escapeString($errfile) . "' " .
                "AND `line` = $errline AND `reported` > " . (time() - 86400));
        if (count($check)) {
            return;
        }
        $bug = $bugRecord;
        $bug['data'] = array('number' => $errno, 'string' => $errstr, 'file' =>
                $errfile, 'line' => $errline, 'reported' => time());
        $dbapi->save($bug);
    }
    mail("mato.jurca@gmail.com", $_SERVER['SERVER_NAME'] . " - bugreport",
            $message, "From: bugreport@" . $_SERVER['SERVER_NAME']);
}
if (Config::BUGREPORT == 'display') {
    error_reporting(-1);
} elseif (Config::BUGREPORT == 'report') {
    error_reporting(E_ERROR | E_PARSE | E_CORE_ERROR | E_COMPILE_ERROR | E_USER_ERROR);
    set_error_handler('bugreporter');
} else {
    error_reporting(0);
}
?>
